﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AssessmentTool.PresentationLogic.Controllers;
using AdminApplication.Controls;
using ModelLayer;
using AssessmentTool.PresentationLogic;
using AdminApplication.ServiceBridge;
using AssessmentTool.Services;
using AssessmentTool.PresentationLogic.EventArguments;
using AssessmentTool.Services.DTO;
using AdminApplication.PresentationLogic.EventArguments;

namespace AdminApplication.PresentationLogic.Controllers
{
    public class ValidationBannerController : AbstractController<ValidationBanner>
    {
        public ValidationBannerController(ValidationBanner element)
            : base(element)
        {
            ed.subscrive("LOGGED-IN", load);
        }

        public void load(EventArgs e)
        {
            UtenteLogged logged = (UtenteLogged)Session.getInstance().getValue("USERLOGGED");
            element.Visibility = logged.grantModificaModello ? Visibility.Visible : Visibility.Collapsed;

            element.Username.Text = ((Utente)Session.getInstance().getValue("USER")).Username;
        }
        public void valida()
        {
            Utente u = (Utente)Session.getInstance().getValue("USER");
            UtenteAdminServiceBridge.valida(u.Sessioni[0], proxy_DoStoricizeCompleted);

        }
        void proxy_DoStoricizeCompleted(object sender, validaCompletedEventArgs e)
        {
            ed.dispatch("STOPBUSY", null);
            if (e.Error == null)
            {
                UtenteLogged u = e.Result;
                UserSelectedEventArg utente = new UserSelectedEventArg() { Utente = new DTOUtente() { Username = u.utente.Username } };
                ed.dispatch("SHOWMESSAGE", new ShowMessageEventArgs() { Testo = "Validazione completata!", Critico = false, Error = false });
                ed.dispatch("CONSOLIDATO", utente);
            }
            else
            {
                ed.dispatch("SHOWMESSAGE", new ShowMessageEventArgs()
                {
                    Testo = "Attenzione! Errore nel validare il modello..\n" +
                        "Se l'errore dovesse ripresentarsi si prega rieffettuare il login.",
                    Error = true,
                    Critico = false
                });
            }
        }
    }
}
